package com.itheima.train.manager.order.mq;

import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class OrderConsumer {

    // 创建日志对象
    private Logger LOGGER = LoggerFactory.getLogger(OrderConsumer.class) ;

    @Autowired
    private StringRedisTemplate redisTemplate ;

    @RabbitListener(queues = "delete_sorted_from_redis")
    public void deleteSortedFromRedis(String msg , Channel channel , Message message) {

        try {

            // 就是从Redis中删除排队信息
            LOGGER.info("【删除排队信息开始】 ----> " + msg);
            redisTemplate.boundZSetOps("train_manager:sorted_queue").remove(msg) ;
            LOGGER.info("【删除排队信息结束】 ----> " + msg);

            // 手动应答
            channel.basicAck(message.getMessageProperties().getDeliveryTag() , false);

        } catch (IOException e) {
            e.printStackTrace();
            LOGGER.info("【删除排队信息出错】 ----> " + msg);
        }

    }
}
